package com.apptest.apptest;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * 1、四大组件（activity、service、content provider、broadcast receiver）都需要在AndroidManifest.xml中注册
 * 2、App入口Activity标识：
 * <intent-filter>
 * <action android:name="android.intent.action.MAIN"/>
 * <p>
 * <category android:name="android.intent.category.LAUNCHER"/>
 * </intent-filter>
 */
public class LaunchActivity extends AppCompatActivity {

    private RecyclerView mRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch);

        //获取控件实例对象
        mRecyclerView = findViewById(R.id.rv);

        //设置为线性布局
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        //设置分割线
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

        List<String> datas = initDatas();
        mRecyclerView.setAdapter(new RVAdapter(this, datas));
    }

    private List<String> initDatas() {
        List<String> list = new ArrayList<>();
        list.add("项目目录简介");
        list.add("页面传值");
        return list;
    }


    private static class RVAdapter extends RecyclerView.Adapter<RVViewHolder> {

        private List<String> list;

        private Context mContext;

        private LayoutInflater mLayoutInflater;

        public RVAdapter(Context context, List<String> list) {
            this.list = list;
            mContext = context;
            mLayoutInflater = LayoutInflater.from(context);
        }

        @NonNull
        @Override
        public RVViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            return new RVViewHolder(mLayoutInflater.inflate(R.layout.item_rv, null, false));
        }

        @Override
        public void onBindViewHolder(@NonNull RVViewHolder rvViewHolder, int i) {
            final String data = getItem(i);
            rvViewHolder.mTextView.setText(data);

            rvViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (data.equals("页面传值")) {
                        IntentActivity.launch(mContext);
                    }else if (data.equals("项目目录简介")){
                        ProjectCatalogueActivity.launch(mContext);
                    }
                }
            });

        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        public String getItem(int position) {
            return list.get(position);
        }
    }

    private static class RVViewHolder extends RecyclerView.ViewHolder {

        TextView mTextView;

        public RVViewHolder(@NonNull View itemView) {
            super(itemView);
            mTextView = itemView.findViewById(R.id.tv_item);
        }
    }


}
